package cn.gson.crm.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.dao.EmpDao;
import cn.gson.crm.model.entity.Dept;
import cn.gson.crm.model.entity.Emp;
import cn.gson.crm.model.entity.Student;
import cn.gson.crm.model.mapper.DeptMapper;
import lombok.extern.log4j.Log4j;

@Controller
@RequestMapping("/dept")
@Log4j
public class DeptController {
	@Autowired
	private DeptMapper deptMapper;
	
	@Autowired
	private DeptDao deptDao;
	
	@Autowired
	private EmpDao empDao;
	
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(Dept dept, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<Dept> pageData = deptMapper.list(dept);

		PageInfo<Dept> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		Dept d;
		if (id != null && (d = deptDao.findOne(id)) != null) {
			d.setDelete(false);
			d.setRemark("不可用");
			obj.put("success", true);
		} else {
			obj.put("message", "此部门不存在！");
		}
		return obj;
	}
	
	@GetMapping({ "add_dept", "add_dept/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			Dept d = deptDao.findOne(id);
			if (d != null) {
				// 如果是编辑，则把原有用户信息传回表单页面
				Iterable<Emp> e=empDao.findByDeptName(d.getName());
				model.addAttribute("e",e);
				model.addAttribute("dept", d);
			}
		}
		Iterable<Emp> emp=empDao.findByRemark("部门经理");
		model.addAttribute("emp",emp);
		return "dept/add_dept";
	}

	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(@Valid Dept dept, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			if (dept.getId() == null) {
				int count=deptMapper.countDeptName(dept.getName());
				if (count>=1) {
					obj.put("message", "该部门已存在");
					return obj;
				}
				
			} else {
				Dept d = deptDao.findOne(dept.getId());
				//编辑的时的两种情况
				if (d.getDeptManager()!=null) {
					//有数据的时候，则表示更新数据
					if(dept.getDeptManager()==null && dept.getDeptManager()=="") {
						dept.setDeptManager(d.getDeptManager());
					}else if(dept.getDeptManager()!=null && dept.getDeptManager()!="") {
						Iterable<Emp> e=empDao.findByDeptName(dept.getName());
						for(Emp em:e) {
							em.setRemark("普通员工");
						}
						Emp emp=empDao.findByName(dept.getDeptManager());
						emp.setRemark("部门经理");
					}
				}
			}
			
			dept.setDelete(true);
			deptDao.save(dept);
			obj.put("success", true);
		}
		return obj;
	}

}
